/************************************************************************************/
/*  Program name CAPEXT_RESP.sql                                                    */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPEXT_RESP';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName1	VARCHAR2(100)	:=	'CAPFND_RESP.dat';
	lv_FileName2	VARCHAR2(100)	:=	'CAPFND_RESP_FUNC.dat';
	lv_FileName3	VARCHAR2(100)	:=	'CAPFND_RESP_TL.dat';

	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	lv_MenuFunc_Name	VARCHAR2(30);

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT
		FR.*,
		FRTL.Responsibility_Name,
		FDG.Data_Group_Name,
		FM.Menu_Name,
		FRG.Request_Group_Name
	FROM
		FND_RESPONSIBILITY		FR,
		FND_RESPONSIBILITY_TL	FRTL,
		FND_DATA_GROUPS			FDG,
		FND_REQUEST_GROUPS		FRG,
		FND_MENUS				FM
	WHERE	FRTL.Responsibility_Name	LIKE	p_NGO||'%'
	AND		FR.Responsibility_id	=		FRTL.Responsibility_id
	AND		FRTL.Language			=		'US'
	AND		FR.Menu_ID				=		FM.Menu_ID
	AND		FR.Data_Group_ID		=		FDG.Data_Group_ID
	AND		FR.Group_Application_ID	=		FRG.Application_ID(+)
	AND		FR.Request_Group_ID		=		FRG.Request_Group_ID(+);

	CURSOR Cur_T(p_NGO VARCHAR2) IS
	SELECT
		*
	FROM
		FND_RESPONSIBILITY_TL
	WHERE	Responsibility_ID	IN	(
				SELECT
					A.Responsibility_ID
				FROM
					FND_RESPONSIBILITY_TL	A
				WHERE	A.Language = 'US'
				AND		A.Responsibility_Name	LIKE p_NGO||'%'
				);

	CURSOR Cur_G(p_NGO VARCHAR2) IS
	SELECT
		FRVL.Responsibility_Key,
		FRF.*
	FROM
		FND_RESP_FUNCTIONS		FRF,
		FND_RESPONSIBILITY_VL	FRVL
	WHERE	FRVL.Responsibility_Name	LIKE	p_NGO||'%'
	AND		FRVL.Responsibility_ID		=		FRF.Responsibility_ID;

BEGIN
	lv_NGO			:=	TRIM(UPPER('&1'));

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_Scc_Count	:=	0;
		lv_Err_Msg		:=	'No Error';
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName1, 'w');
		FOR C1 IN Cur_F(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
					C1.Application_ID||CHR(9)||
					C1.Responsibility_ID||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Creation_Date||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
					C1.Data_Group_Application_ID||CHR(9)||
					C1.Data_Group_ID||CHR(9)||
					C1.Menu_ID||CHR(9)||
				C1.Term_Security_Enabled_Flag||CHR(9)||
				C1.Start_Date||CHR(9)||
				C1.End_Date||CHR(9)||
					C1.Group_Application_ID||CHR(9)||
					C1.Request_Group_ID||CHR(9)||
				C1.Version||CHR(9)||
				C1.Web_Host_Name||CHR(9)||
				C1.Web_Agent_Name||CHR(9)||
				C1.Responsibility_Key||CHR(9)||
				CAPBREXT_COMMON.GetAppName(C1.Application_ID)||CHR(9)||
				C1.Responsibility_Name||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)||CHR(9)||
				CAPBREXT_COMMON.GetAppName(C1.Data_Group_Application_ID)||CHR(9)||
				C1.Data_Group_Name||CHR(9)||
				C1.Menu_Name||CHR(9)||
				C1.Request_Group_Name
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Responsibilities for this NGO';
		END IF;
		UTL_FILE.FClose(lv_UTL_File);
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAP_RESP',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
/******************************************************************/
		lv_Scc_Count	:=	0;
		lv_Err_Msg		:=	'No Error';
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName2, 'w');
		FOR C2 IN Cur_G(lv_NGO) LOOP
			IF	C2.Rule_Type	=	'M'	THEN
				lv_MenuFunc_Name	:=	CAPBREXT_COMMON.GetMenuName(C2.Action_ID);
			ELSE
				lv_MenuFunc_Name	:=	CAPBREXT_COMMON.GetFormFunc(C2.Action_ID);
			END IF;

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
					C2.Application_ID||CHR(9)||
					C2.Responsibility_ID||CHR(9)||
					C2.Action_ID||CHR(9)||
				C2.Rule_Type||CHR(9)||
				C2.Last_Update_Date||CHR(9)||
					C2.Last_Updated_By||CHR(9)||
				C2.Last_Update_Login||CHR(9)||
				C2.Creation_Date||CHR(9)||
					C2.Created_By||CHR(9)||
				CAPBREXT_COMMON.GetAppName(C2.Application_ID)||CHR(9)||
				C2.Responsibility_Key||CHR(9)||
				lv_MenuFunc_Name||CHR(9)||
				CAPBREXT_COMMON.GetUser(C2.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C2.Created_By)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Responsibilities Function for this NGO';
		END IF;
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAP_RESP_FUNC',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
		UTL_FILE.FClose(lv_UTL_File);
/******************************************************************/
		lv_Scc_Count	:=	0;
		lv_Err_Msg		:=	'No Error';
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName3, 'w');
		FOR C3 IN Cur_T(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C3.Application_ID||CHR(9)||
				C3.Responsibility_ID||CHR(9)||
				C3.Language||CHR(9)||
				C3.Responsibility_Name||CHR(9)||
					C3.Created_By||CHR(9)||
				C3.Creation_Date||CHR(9)||
					C3.Last_Updated_By||CHR(9)||
				C3.Last_Update_Date||CHR(9)||
				C3.Last_Update_Login||CHR(9)||
				C3.Description||CHR(9)||
				C3.Source_Lang||CHR(9)||
				CAPBREXT_COMMON.GetAppName(C3.Application_ID)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C3.Created_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C3.Last_Updated_By)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Responsibilities Translation for this NGO';
		END IF;
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAP_RESP_TL',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';

		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID,
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	END IF;

	COMMIT;
END;
/